package classespinkpony.validators
{
	import classespinkpony.User;
	
	import mate.events.RegistrationEvent;
	
	import mx.validators.ValidationResult;
	import mx.validators.Validator;

	public class UsernameValidator extends Validator
	{
		public function UsernameValidator()
		{
			super();
		}
		
		public var username:String;
		public var noMatchError:String = "Username bestaat al!";
		private var _user:User;
		
		[Bindable]
		public function get user():User
		{
			return _user;
		}

		public function set user(value:User):void
		{
			_user = value;
		}

		override protected function doValidation(value:Object):Array{
			username = String(value);
			var ValidatorResults:Array = new Array();
			ValidatorResults = super.doValidation(value);
			
			var checkUserAlreadyExists:RegistrationEvent = new RegistrationEvent(RegistrationEvent.CHECKUSERALREADYEXISTS);
			checkUserAlreadyExists.searchString = username;
			dispatchEvent(checkUserAlreadyExists);
			
			if(user != null){
				ValidatorResults.push(new ValidationResult(true, null, "Username error", noMatchError));
				return ValidatorResults;
			}
			
			return ValidatorResults;
		}
	}
}